<template>
  <el-container class="home-container">
    <!-- 这些组件名称即可当类名使用 用.el-container即可为其添加样式 -->
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出登录</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
      <div class="toggle-button" @click="toggleCollapse">|||</div>
      <!-- 注意下面的router属性 本来开启router功能可以将index作为地址 -->
      <el-menu
       router
       class="el-menu-vertical-demo"
       :unique-opened="true" :collapse="isCollapse" :collapse-transition="false" :default-active="activePath">
       <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
         <template slot="title">
           <i :class="icondata[item.id]"></i>
           <span>{{item.authName}}</span>
         </template>
         <!-- 二级菜单 -->
         
           <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
            <template slot="title">
             <i class="el-icon-menu"></i>
             <span>{{subItem.authName}}</span>
           </template>
           </el-menu-item>
       </el-submenu>
    </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data(){
    return {
      // 左侧菜单数据
      menulist: [],
      icondata:{
        '125':'el-icon-user-solid',
        '103':'el-icon-s-grid',
        '101':'el-icon-shopping-bag-2',
        '102':'el-icon-s-order',
        '145':'el-icon-data-line'
      },
      isCollapse:false,
      // 被激活的链接地址
      activePath: '',
    }
  },
  created() {
    //生命周期函数 页面生成时获取后端菜单数据
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods:{
    logout(){
      //清除token（将sessionStorage保存的token清除）并编程式跳转到login页面
      //将token储存到sessionStorage的代码是 "window.sessionStorage.setItem('token', res.data.token)"
       window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单 const{data:res}是固定写法 用解构赋值获取被后端包裹的数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data//将菜单数据赋值给vue实例data里的menulist 
      console.log(res)
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
  }

}
</script>

<style lang="less"scoped>
.home-container{
  height: 100%;
  background-color: rgb(246,246,246);
}
.el-header{
  background-image: linear-gradient(to right,rgb(114, 135, 254),rgb(130, 88, 186),rgb(59, 45, 159));//渐变色
  box-shadow: 0px 2px 3px rgb(146, 141, 141);
  margin-bottom: 6px;
  color: rgb(255, 255, 255);
  font-weight: bold;
  font-size: 20px;
  .el-button{
   position: absolute;
   right: 20px;
   top: 10px;
   box-shadow: 2px 2px 3px rgb(63, 61, 61);
  }
    > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
    }

}
.el-aside{
  background-color: #fff;
  margin-right: 5px;
  .toggle-button{
    font-weight: 900;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
    font-size: 15px;
    box-shadow: 2px 2px 3px rgb(209, 201, 201);
    margin-bottom: 6px;
    padding: 2px 0 2px;
    color: rgb(105, 96, 96);
  }
}
.toggle-button:hover{
  transform: scale(1.2,1.2);
  transition: 0.5s;
}
.el-menu{
  border-right: none;
}
.el-main{
  background-color: #fff;
}

</style>